
package com.starshineworks.util;

import com.starshineworks.entities.*;
import java.io.*;
import java.util.*;
import jxl.read.biff.BiffException;

public class DataMatrixElab {

    //private File inputWorkbook;
    private ArrayList<ArrayList<Object>> _matrix;
    
    DataMatrixElab(List<String[]> m) {        
        this._matrix = new ArrayList<ArrayList<Object>>();
        for (int r = 0; r < m.size(); r++) {
            ArrayList<Object> row = new ArrayList<Object>();
            for (int c = 0; c < m.get(r).length; c++) {
                String s = m.get(r)[c];
                row.add(c, s);
            }
            this._matrix.add(r, row);
        }
    }
 
    public boolean updateDb(Class type) throws Exception {
        if (_matrix.size() < 2) {
            return false;
        }
        try {
            ArrayList<Object> head = _matrix.get(0);
            int headSize = head.size();
            // Do names
            /* XXX
            for (int c = 0; c < headSize; c++) {
                String nm = ((String)head.get(c)).toLowerCase();
                map.put(nm, null);
            }*/
            // Do values
            for (int r = 1; r < _matrix.size(); r++) {
                List row = _matrix.get(r);
                HashMap<String,String[]> map = new HashMap<String,String[]>();
                for (int c = 0; c < headSize; c++) {
                    String nm = ((String)head.get(c)).toLowerCase();
                    nm = "_" + nm;
                    if (nm != null) {
                        if (nm != "") {
                            String cell = null;
                            String[] ss = null;
                            if (row.size() >= headSize) {
                                cell = (String)row.get(c);
                                // Camel-case items except for name
                                ss = Util.splitStringToArray(cell, (c != 0));
                            }
                            map.put(nm, ss);
                        }
                    }
                }
                EntityE.create(type, map);
            }
        } catch (Exception e) {
            Util.handleException(e);
            return false;
        }        
        return true;
    }
    
    public String toHTML() throws Exception {
        String y = "";
        if (_matrix.size() < 2) {
            return "<p>**ERROR**</p>";
        }
        try {
            y = "<table>";
            List head = (List)_matrix.get(0);
            int headSize = head.size();
            // Do names
            y += "<tr>";
            for (int c = 0; c < headSize; c++) {
                y += "<td>" + head.get(c) + "</td>";
            }
            y += "</tr>";
            // Do values
            for (int r = 1; r < _matrix.size(); r++) {
                y += "<tr>";
                List row = (List)_matrix.get(r);
                int rowSize = headSize;
                int usedSize = Math.min(rowSize, headSize);
                for (int c = 0; c < usedSize; c++) {
                    String cell = (String)row.get(c);
                    int x = 0;
                    x++;
                    y += "<td>" + row.get(c) + "</td>";
                }
            }
            y += "</tr>";
            y += "</table>";
        } catch (Exception e) {
            Util.handleException(e);
            return "<p>***ERROR***</p>";
        }        
        return y;
    }

    /* XXXX
    public boolean fillRow(String  type, Map<String, String[]> tMap) {
        //EntityDAO.createDAO(type).setObjectData(tMap);
        return true;
    }
    
    public boolean fillDAO(ArrayList<ArrayList<String>> matrix, 
                           String type, Integer id) {
        Integer nRow = 0;
        ArrayList<String> first = matrix.get(0);
        Iterator itr = matrix.iterator();
        while (itr.hasNext()) {
            ArrayList<String> row = (ArrayList<String>) itr.next();
            nRow++;
            if (nRow == 1) {
                continue;
            }
            Integer nCol = 0;
            Iterator itc = row.iterator();
            HashMap<String, String[]> tMap = new HashMap<String, String[]>();
            while (itc.hasNext()) {
                String name = (String) first.get(nCol).toLowerCase();
                String cell = (String) itc.next();
                String[] val = new String[1];
                val[0] = cell;
                tMap.put(name, val);
                nCol++;
            }
            fillRow(type, tMap);
        }
        return nRow > 0;
    }*/
        
}
